package com.abner.springcloudprovideruser.Controller;

import com.abner.springcloudprovideruser.Service.UserService;
import com.abner.springcloudprovideruser.dao.UserMapper;
import com.abner.springcloudprovideruser.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Abner.Jiang
 * @Description:
 * @Date: Created on 2019/12/10 10:58
 * @ProjectName: springcloud-provider-user
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/get/{id}")
    public User get(@PathVariable String id){
        log.info("start to query User , id : [{}]",id);
        User user = null;
        try {
            user = userService.getUser(id);
            log.info("success to query user ! result is : [{}]",user);
        }catch (Exception e){
            log.error("quey user is error ! id : [{}],exception is : [{}]",id,e);
        }
        return user;
    }

    @GetMapping("/get")
    public User get(@RequestParam("id") String id,@RequestParam("username")  String username){
        log.info("start to query User , id : [{}],username:[{}]",id,username);
        User user = null;
        try {
            user = userService.getUserByIdAndUsername(id,username);
            log.info("success to query user ! result is : [{}]",user);
        }catch (Exception e){
            log.error("quey user is error ! id : [{}],exception is : [{}]",id,e);
        }
        return user;
    }

//    @GetMapping("/get")
//    public User get(@RequestParam("id") String id,@RequestParam("username")  String username){
//        log.info("start to query User , id : [{}],username:[{}]",id,username);
//        User user = null;
//        try {
//            user = userService.getUserByIdAndUsername(id,username);
//            log.info("success to query user ! result is : [{}]",user);
//        }catch (Exception e){
//            log.error("quey user is error ! id : [{}],exception is : [{}]",id,e);
//        }
//        return user;
//    }

    @PostMapping("/add")
    public String addUser(@RequestBody User user){
        log.info("start to add User , user : [{}]",user);
        String result = null;
        try {
            int i = userService.addUser(user);
            if (i>0) {
                result = "success";
                log.info("success to query user ! result is : [{}]", user);
            } else {
                result = "failed";
                log.info("failed to query user ! result is : [{}]", user);
            }
        }catch (Exception e){
            log.error("quey user is error ! user : [{}],exception is : [{}]",user,e);
        }
        return result;
    }

}
